package com.zhuang.chapter15;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class FileCopy {
	
	public static void main(String[] args) {
		copyFile();
	}

	public static void copyFile() {
		System.out.println("请输入源文件名 例如 f:\\\\1.txt");
		Scanner scanner = new Scanner(System.in);
		String srcFilePath = scanner.nextLine();
		System.out.println("请输入目标文件名 例如 f:\\\\1.txt");
		String desFilePath = scanner.nextLine();

		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(srcFilePath);
			fos = new FileOutputStream(desFilePath);
			// 定义字节数组
			byte[] buf = new byte[1024];
			int len = 0;
			while ((len = fis.read(buf)) != -1) {
				// 一遍读一边写
				fos.write(buf, 0, len);
			}
			System.out.println("文件拷贝成功！！！");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fis.close();
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			scanner.close();
		}
	}
}
